findAllCompetitions();
fundAllTabs();

//所有赛事
var competition_list;

//每页显示条数
var page_count = 15;

/**
 * 弹框隐藏后，重置
 */
$('#loginModal').on('hidden.bs.modal', function (e) {

    document.getElementById("competition_span").removeAttribute("competitionId")
    document.getElementById("competition_span").removeAttribute("competitionName");
    document.getElementById("competition_span").removeAttribute("refresh_id");

    var checks = document.getElementsByName("tab_checkbox");

    for (var i = 0; i < checks.length; i++) {
        var ch = checks[i];
        ch.checked = false;
    }
})

/**
 *  设置标签
 */
function setTabAction(element) {
    var competitionId = element.getAttribute("competitionId");
    var competitionName = element.getAttribute("competitionName");
    var refresh_id = element.getAttribute("refresh_id");
    var competitionTab = document.getElementById(refresh_id).getAttribute("competitionTab");

    if (competitionTab.endsWith(',')) {
        competitionTab = competitionTab.substring(0, competitionTab.length - 1);
    }
    var tabs = competitionTab.split(',');

    var checks = document.getElementsByName("tab_checkbox");

    for (var i = 0; i < checks.length; i++) {
        var ch = checks[i];

        var tabId = ch.getAttribute("tabId");

        if (tabs.indexOf(tabId) > -1) {
            ch.checked = true;
        }
    }

    document.getElementById("competition_span").setAttribute("competitionId", competitionId);
    document.getElementById("competition_span").setAttribute("competitionName", competitionName);
    document.getElementById("competition_span").setAttribute("refresh_id", refresh_id);

}

/**
 * 保存设置的标签
 */
function saveTabs() {

    var competitionId = document.getElementById("competition_span").getAttribute("competitionId")
    var competitionName = document.getElementById("competition_span").getAttribute("competitionName");
    var refresh_id = document.getElementById("competition_span").getAttribute("refresh_id");

    var checks = document.getElementsByName("tab_checkbox");

    var tabs = '';

    var tabNames = '';

    var tabArray = [];

    for (var i = 0; i < checks.length; i++) {
        var ch = checks[i];
        if (ch.checked == true) {
            var tabId = ch.getAttribute("tabId");
            var tabName = ch.getAttribute("tabName");

            tabs += tabId + ',';
            tabNames += tabName + ',';
            tabArray.push({
                "tabId":tabId,
                "tabName":tabName
            });
        }
    }

    if (tabs.endsWith(',')) {
        tabs = tabs.substring(0, tabs.length - 1);
    }

    if (tabs.length == 0) {
        tabs = '0';
    }

    var data = {
        'competitionId': competitionId,
        'competitionName': competitionName,
        'competitionTab': tabs,
    }

    $.ajax({
        url: baseUrl.base + '/competition/setCompetition',
        type: 'get',
        data: data,
        success: function (data) {
            if (data.code == 0) {
                showNotification('bottom', 'right', '成功');
                document.getElementById(refresh_id).innerText = tabNames;
                document.getElementById(refresh_id).setAttribute("competitiontab",tabs);

                for (var i = 0;i < competition_list.length;i ++){
                    var competition = competition_list[i];
                    if (competitionId == competition["competitionId"]){
                        competition["tabs"] = tabArray;
                        break;
                    }
                }

            } else {
                showNotification('bottom', 'right', '失败');
            }
            $('#loginModal').modal('hide');
        }
    })
}


/**
 * 获取所有没有被禁用的标签
 */
function fundAllTabs() {
    $.ajax({
        url: baseUrl.base + 'customTab/getTabList',
        type: 'post',
        data: {'isEnable': 0},
        success: function (data) {
            if (data.code == 0) {
                $("#tab_table_body").html("");
                for (var i = 0; i < data.results.length; i++) {
                    var tab = data.results[i];

                    var tabName = tab.tabName;
                    var tabId = tab.tabId;

                    var tr = document.createElement("tr");
                    var td = document.createElement("td");
                    var div = document.createElement("div");
                    div.setAttribute("class", "form-check");
                    div.setAttribute("style", "padding-top: 1rem");

                    var label = document.createElement("label");
                    label.setAttribute("class", "form-check-label");

                    var input = document.createElement("input");
                    input.setAttribute("class", "form-check-input");
                    input.setAttribute("type", "checkbox");
                    input.setAttribute("name", "tab_checkbox");
                    input.setAttribute("tabId", tabId);
                    input.setAttribute("tabName", tabName);

                    label.appendChild(input);

                    var span1 = document.createElement("span");
                    span1.setAttribute("class", "form-check-sign");
                    label.appendChild(span1);

                    var span2 = document.createElement("span");
                    span2.setAttribute("class", "check");
                    span1.appendChild(span2);

                    div.appendChild(label);
                    td.appendChild(div);
                    tr.appendChild(td);

                    var td = document.createElement("td");
                    var tdContent = document.createTextNode(tabName);
                    td.appendChild(tdContent);
                    tr.appendChild(td);

                    document.getElementById("tab_table_body").append(tr);
                    document.getElementById("tab_table_body").innerHTML;

                    document.getElementById('tab_select').add(new Option(tabName, tabId));
                    $("#tab_select").selectpicker('refresh');
                }
            }
        }
    })
}


/**
 * 启用禁用赛事
 */
function setDisableAction(element) {
    var data = {
        'competitionId': element.getAttribute("competitionId"),
        'competitionName': element.getAttribute("competitionName"),
        'isEnable': element.getAttribute("status")
    }

    $.ajax({
        url: baseUrl.base + '/competition/setCompetition',
        type: 'get',
        data: data,
        success: function (data) {
            if (data.code == 0) {

                if (element.getAttribute("status") == 1) {
                    //已禁用变为已启用
                    element.setAttribute("class", "btn btn-danger btn-sm");
                    element.setAttribute("status", 0);
                    element.onclick = function (ev) {
                        setDisableAction(this);
                    }
                    element.innerText = "已禁用";
                    showNotification('bottom', 'right', '禁用成功');
                } else {
                    ////已启用变为禁用
                    element.setAttribute("class", "btn btn-info btn-sm");
                    element.setAttribute("status", 1);
                    element.onclick = function (ev) {
                        setDisableAction(this);
                    }
                    element.innerText = "已启用";
                    showNotification('bottom', 'right', '已启成功');
                }
            }
        }
    });
}

/**
 * 搜索按钮
 */
function searchAction() {
    findAllCompetitions();
}

/**
 * 获取所有的赛事列表
 */


function findAllCompetitions() {

    var status = $('#status_select').val();
    var tabId = $('#tab_select').val();
    var keyword = $('#keyword').val();

    var data = {};

    if (null != status && status.length != 0) {
        data['status'] = status;
    }
    if (null != tabId && tabId.length != 0) {
        data['tabId'] = tabId;
    }
    if (null != keyword && keyword.length != 0) {
        data['keyword'] = keyword;
    }

    $.ajax({
            url: baseUrl.base + '/country/competitionAllList',
            type: 'get',
            data: data,
            success: function (data) {

                if (data.code == "0") {

                    var res = data.results;

                    competition_list = res;

                    cutPagination(1);

                    // 配置分页
                    $("#p1").pagination({
                        totalData: res.length,
                        showData:page_count,
                        callback: function (idx) {
                            cutPagination(idx);
                        }
                    });
                }
            }
        }
    )
};

/**
 * 分页
 * @param page 从1开始
 */
function cutPagination(page) {

    var start = (page - 1) * page_count;
    var end = start + page_count;

    if (end > competition_list.length){
        end = competition_list.length;
    }

    $("#tbody").html("");

    for (var i = start; i < end; i++) {

        var competition = competition_list[i];
        var competitionId = competition.competitionId;
        var competitionName = competition.competitionNameZh;
        var competitionNameEn = competition.competitionNameEn;
        var countryName = competition.countryNameZh;
        var tabs = competition.tabs;
        var competitionTab = competition.competitionTab;
        var enabled = competition.enabled;

        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var tdContent = document.createTextNode(competitionId);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(competitionName);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        var tdContent = document.createTextNode(countryName);
        td.appendChild(tdContent);
        tr.appendChild(td);

        var td = document.createElement("td");
        td.setAttribute("id", 'tab_names_id_' + i);

        var tab_names = '';
        if (tabs != null) {

            for (var j = 0; j < tabs.length; j++) {
                tab_names = tab_names + tabs[j].tabName + ',';
            }
        }
        var tdContent = document.createTextNode(tab_names);
        td.appendChild(tdContent);
        td.setAttribute("competitionTab", competitionTab);
        tr.appendChild(td);

        var td = document.createElement("td");
        var btn1 = document.createElement("button");
        btn1.setAttribute("type", "button");
        btn1.setAttribute("class", "btn btn-info btn-sm");
        btn1.setAttribute("data-toggle", "modal");
        btn1.setAttribute("data-target", "#loginModal");
        btn1.setAttribute("competitionId", competitionId);
        btn1.setAttribute("competitionName", competitionNameEn);
        btn1.setAttribute("refresh_id", 'tab_names_id_' + i);

        btn1.onclick = function (ev) {
            setTabAction(this);
        }
        var tdContent = document.createTextNode("设置标签");
        btn1.appendChild(tdContent);
        td.appendChild(btn1);

        var btn2 = document.createElement("button");
        btn2.setAttribute("type", "button");
        btn2.setAttribute("competitionId", competitionId);
        btn2.setAttribute("competitionName", competitionName);

        if (enabled == 1) {
            //已禁用
            btn2.setAttribute("class", "btn btn-danger btn-sm");
            var tdContent = document.createTextNode("已禁用");
            btn2.setAttribute("status", 0);
            btn2.onclick = function (ev) {
                setDisableAction(this);
            }
        } else {
            //已启用
            btn2.setAttribute("class", "btn btn-info btn-sm");
            var tdContent = document.createTextNode("已启用");
            btn2.setAttribute("status", 1);
            btn2.onclick = function (ev) {
                setDisableAction(this);
            }
        }

        btn2.appendChild(tdContent);
        td.appendChild(btn2);

        tr.appendChild(td);

        document.getElementById("tbody").append(tr);
        document.getElementById("tbody").innerHTML;
    }

}